﻿$(function() {
    $("#txtUserName").focus();
    $(".loading").css("display", "none"); //初始化页面
    $(".loading").css("height", $(document).height());
    $("#btnLogin").click(function() {
        $(".loading").css("display", "block"); //等待登录
        var name = $("#txtUserName").val();
        var pwd = $("#txtUserPwd").val();
        var code = $("#txtCode").val();
        if (name == "" || name == null) {
            alert('请输入用户名!');
            $("#txtUserName").focus();
            $(".loading").css("display", "none");
        } else {
            if (pwd == "" || pwd == null) {
                alert('请输入登录密码!');
                $(".loading").css("display", "none");
                $("#txtUserPwd").focus();
            } else {
                if (code == "" || code == null) {
                    alert('请输入验证码!');
                    $(".loading").css("display", "none");
                    $("#txtCode").focus();
                } else {
                    $.ajax({
                        type: "POST",
                        contentType: "application/x-www-form-urlencoded",
                        url: "../Ajax/Login_Ajax.aspx?" + Math.random(),
                        data: {
                            "action": "login",
                            "name": escape(name),
                            "pwd": escape(pwd),
                            "code": escape(code)
                        },
                        success: function(msg) {
                            if (msg == '0') {
                                alert('验证码错误,请重新输入!');
                                $("#imgCode").click();
                                $("#txtCode").val("");
                                $("#txtCode").focus();
                                $(".loading").css("display", "none");
                            } else if (msg == '1') {
                                alert('用户名错误,请重新输入!');
                                $("#txtUserName").val("");
                                $("#txtUserName").focus();
                                $(".loading").css("display", "none");
                            } else if (msg == '2') {
                                alert('密码错误,请重新输入!');
                                $("#txtUserPwd").val("");
                                $("#txtUserPwd").focus();
                                $(".loading").css("display", "none");
                            } else {
                                window.location.href = '../Admin/Default.aspx';
                            }
                        },
                        error: function(xhr) {
                            alert(xhr.responseText);
                            $(".loading").css("display", "none");
                        }
                    });
                }
            }
        }
    });
})
//--------键盘按下回车后登陆
$(function() {
    if (document.addEventListener) {
        document.addEventListener("keypress", fireFoxHandler, true)
    } else {
        document.attachEvent("onkeypress", ieHandler);
    }
    function fireFoxHandler(evt) {
        if (evt.keyCode == 13) {
            $("#btnLogin").click()
        }
    }
    function ieHandler(evt) {
        if (evt.keyCode == 13) {
            $("#btnLogin").click()
        }
    }
})